package de.berlani.geolike;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class GeoLikeActivity extends MapActivity {
	MapView mapView;
	GeoUpdater geoUpdater;
	private MyLocationOverlay myLocationOverlay;
	private Map<String, String> geodata = new HashMap<String,String>();
	
	private ServerLink serverLink = new JSONServerLink();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        geoUpdater = new GeoUpdater(this);
        
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        
        myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
		mapView.getOverlays().add(myLocationOverlay);
		mapView.getController().setZoom(13);
        
        String phoneid = MD5_Hash(Settings.Secure.ANDROID_ID+((String) android.os.Build.BOARD)+((String) android.os.Build.DEVICE)+((String) android.os.Build.ID));
        geodata.put("phoneid", phoneid);
               
        // Buttons initialisieren
        final Button btn_like = (Button)findViewById(R.id.btn_like);
        final Button btn_dislike = (Button)findViewById(R.id.btn_dislike);
        
        btn_like.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                commit(true);
            }
        });
        btn_dislike.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		commit(false);
        	}
        });
                    
    }
    
    @Override
    protected void onResume(){
    	super.onResume();
    	geoUpdater.resume();
    }
    
    @Override
	protected void onPause() {
		super.onPause();
		geoUpdater.pause();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void update(Map<String,String> newgeodata) {
		geodata.putAll(newgeodata); //Old Attributes will hopefully be overwritten
		if(mapView!=null){
			mapView.getController().setCenter(geoUpdater.getGeoPoint());
		}
		
	}
	
	private static String MD5_Hash(String s) {
		MessageDigest m = null;
		try {
            m = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
		}
		m.update(s.getBytes(),0,s.length());
		String hash = new BigInteger(1, m.digest()).toString(16);
		return hash;
	}
	
	private void commit(Boolean liked){
		/* I just noticed that you might can dismiss this whole GeoUpdater thingy and just do this:
		 *  Location lastFix = myLocationOverlay.getLastFix();
		 *  DAMM!!!!
		 */
		
		
		geodata.put("liked",String.valueOf(liked));
		
		// Make this a Thread/Task so we can have a Progress Indicator
		try {
			serverLink.Send(geodata);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			toste("ClientProtocolException");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			toste("IOException");
		}
		
		//Dialog Initialisieren
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.dialogexit);
		dialog.setTitle("Exit?");
		dialog.show();
		Button btn_exit = (Button)dialog.findViewById(R.id.btn_exit);
        Button btn_stay = (Button)dialog.findViewById(R.id.btn_stay);
        
        btn_exit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                quit();
            }
        });
        
        btn_stay.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		dialog.dismiss();
        	}
        });
        
        
		
		
	}
	
	public void toste(String text){
		Toast.makeText(this, text,Toast.LENGTH_LONG ).show();
	}
	
	private void quit(){
		finish();
	}
}